<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
  
  <HEAD><META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type">
    <TITLE>simplejson.JSONEncoder -- Extensible JSON &amp;lt;&lt;a class="reference" href="http://json.org">http://json.org&lt;/a>&amp;gt; encoder for Python data structures.</TITLE>
    <LINK HREF="layout.css" TYPE="text/css" REL="stylesheet">
  </HEAD>
  <BODY>
    <DIV ID="page">
      
    <DIV ID="top-nav">
        <H1 ID="doc-title"><A HREF="index.html" REL="index">simplejson 1.9</A></H1>
        <DIV CLASS="online-navigation">
          <A HREF="index.html" REL="index">index</A>
          <SPAN ID="nav-docs">
            
          </SPAN>
          
          

          <BR>

          <A HREF="module-simplejson.html" TITLE="simplejson reference">
            simplejson
          </A>

          
            
            <A HREF="class-simplejson.JSONEncoder.html">details</A>
            
            <A HREF="class-simplejson.JSONEncoder-index.html">
              tree
            </A>
            
          
        </DIV>
    </DIV>
    
    <DIV ID="main-content">
  
  <H1 CLASS="pudge-member-page-heading">
    <TT>JSONEncoder</TT>
  </H1>
  <H4 CLASS="pudge-member-page-subheading">
    Extensible JSON &lt;<A HREF="http://json.org" CLASS="reference">http://json.org</A>> encoder for Python data structures.
  </H4>
  <P CLASS="pudge-member-parent-link">
    <SMALL>
    The JSONEncoder class is accessible via the
    <A HREF="module-simplejson.html">
      <TT>simplejson</TT>
    </A> module.
    </SMALL>
  </P>
  <DIV ID="pudge-section-nav">
  <UL>
    <LI>
      
      <SPAN CLASS="pudge-missing-section-link">
        Attributes
      </SPAN>
    </LI><LI>
      <A HREF="#methods" CLASS="pudge-section-link">
        Methods (4)
      </A>
      
    </LI>
    
    <LI>
      <A HREF="simplejson/encoder.py.html?f=83&amp;l=382#83" CLASS="pudge-section-link">
        Source
      </A>
    </LI>
  </UL>
  </DIV>
  <DIV STYLE="clear: left"></DIV>
  <DIV CLASS="rst pudge-module-doc">
    <P>Supports the following objects and types by default:</P>
<TABLE BORDER="1" CLASS="docutils">
<COLGROUP>
<COL WIDTH="56%">
<COL WIDTH="44%">
</COLGROUP>
<THEAD VALIGN="bottom">
<TR><TH CLASS="head">Python</TH>
<TH CLASS="head">JSON</TH>
</TR>
</THEAD>
<TBODY VALIGN="top">
<TR><TD>dict</TD>
<TD>object</TD>
</TR>
<TR><TD>list, tuple</TD>
<TD>array</TD>
</TR>
<TR><TD>str, unicode</TD>
<TD>string</TD>
</TR>
<TR><TD>int, long, float</TD>
<TD>number</TD>
</TR>
<TR><TD>True</TD>
<TD>true</TD>
</TR>
<TR><TD>False</TD>
<TD>false</TD>
</TR>
<TR><TD>None</TD>
<TD>null</TD>
</TR>
</TBODY>
</TABLE>
<P>To extend this to recognize other objects, subclass and implement a
<TT CLASS="docutils literal"><SPAN CLASS="pre">.default()</SPAN></TT> method with another method that returns a serializable
object for <TT CLASS="docutils literal"><SPAN CLASS="pre">o</SPAN></TT> if possible, otherwise it should call the superclass
implementation (to raise <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeError</SPAN></TT>).</P>

  </DIV>
  
  <HR>
  
  
    <A NAME="methods"></A>
    <H2>Methods</H2>
    <DIV CLASS="pudge-member routine alias">
    <A NAME="__init__"></A>
    <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN>
      <TT><A HREF="class-simplejson.JSONEncoder.html#__init__" CLASS="pudge-obj-link">__init__</A>(self, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, encoding='utf-8', default=None)</TT>
      <A HREF="simplejson/encoder.py.html?f=115&amp;l=173#115" TITLE="View Source">...</A>
    </H4>
    <DIV CLASS="pudge-section rst">
      
    <P CLASS="pudge-member-blurb">
      Constructor for JSONEncoder, with sensible defaults.
    </P>
    <P>If skipkeys is False, then it is a TypeError to attempt
encoding of keys that are not str, int, long, float or None.  If
skipkeys is True, such items are simply skipped.</P>
<P>If ensure_ascii is True, the output is guaranteed to be str
objects with all incoming unicode characters escaped.  If
ensure_ascii is false, the output will be unicode object.</P>
<P>If check_circular is True, then lists, dicts, and custom encoded
objects will be checked for circular references during encoding to
prevent an infinite recursion (which would cause an OverflowError).
Otherwise, no such check takes place.</P>
<P>If allow_nan is True, then NaN, Infinity, and -Infinity will be
encoded as such.  This behavior is not JSON specification compliant,
but is consistent with most JavaScript based encoders and decoders.
Otherwise, it will be a ValueError to encode such floats.</P>
<P>If sort_keys is True, then the output of dictionaries will be
sorted by key; this is useful for regression tests to ensure
that JSON serializations can be compared on a day-to-day basis.</P>
<P>If indent is a non-negative integer, then JSON array
elements and object members will be pretty-printed with that
indent level.  An indent level of 0 will only insert newlines.
None is the most compact representation.</P>
<P>If specified, separators should be a (item_separator, key_separator)
tuple.  The default is (', ', ': ').  To get the most compact JSON
representation you should specify (',', ':') to eliminate whitespace.</P>
<P>If specified, default is a function that gets called for objects
that can't otherwise be serialized.  It should return a JSON encodable
version of the object or raise a <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeError</SPAN></TT>.</P>
<P>If encoding is not None, then all input strings will be
transformed into unicode using that encoding prior to JSON-encoding.
The default is UTF-8.</P>

  
    </DIV>
  </DIV><DIV CLASS="pudge-member routine alias">
    <A NAME="default"></A>
    <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN>
      <TT><A HREF="class-simplejson.JSONEncoder.html#default" CLASS="pudge-obj-link">default</A>(self, o)</TT>
      <A HREF="simplejson/encoder.py.html?f=323&amp;l=342#323" TITLE="View Source">...</A>
    </H4>
    <DIV CLASS="pudge-section rst">
      
    <P CLASS="pudge-member-blurb">
      Implement this method in a subclass such that it returns
a serializable object for <TT CLASS="docutils literal"><SPAN CLASS="pre">o</SPAN></TT>, or calls the base implementation
(to raise a <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeError</SPAN></TT>).
    </P>
    <P>For example, to support arbitrary iterators, you could
implement default like this:</P>
<PRE CLASS="literal-block">
def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    return JSONEncoder.default(self, o)
</PRE>

  
    </DIV>
  </DIV><DIV CLASS="pudge-member routine alias">
    <A NAME="encode"></A>
    <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN>
      <TT><A HREF="class-simplejson.JSONEncoder.html#encode" CLASS="pudge-obj-link">encode</A>(self, o)</TT>
      <A HREF="simplejson/encoder.py.html?f=343&amp;l=366#343" TITLE="View Source">...</A>
    </H4>
    <DIV CLASS="pudge-section rst">
      
    <P CLASS="pudge-member-blurb">
      Return a JSON string representation of a Python data structure.
    </P>
    <PRE CLASS="doctest-block">
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})
'{"foo": ["bar", "baz"]}'
</PRE>

  
    </DIV>
  </DIV><DIV CLASS="pudge-member routine alias">
    <A NAME="iterencode"></A>
    <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN>
      <TT><A HREF="class-simplejson.JSONEncoder.html#iterencode" CLASS="pudge-obj-link">iterencode</A>(self, o)</TT>
      <A HREF="simplejson/encoder.py.html?f=367&amp;l=382#367" TITLE="View Source">...</A>
    </H4>
    <DIV CLASS="pudge-section rst">
      
    <P CLASS="pudge-member-blurb">
      Encode the given object and yield each string
representation as available.
    </P>
    <P>For example:</P>
<PRE CLASS="literal-block">
for chunk in JSONEncoder().iterencode(bigobject):
    mysocket.write(chunk)
</PRE>

  
    </DIV>
  </DIV>
    
  
  
  <P>
    <SMALL>
    
    
    See 
    <A HREF="simplejson/encoder.py.html?f=83&amp;l=382#83" TITLE="simplejson/encoder.py:83">the source</A>
    for more information.
    </SMALL>
  </P>

</DIV>
    
    <DIV ID="footer">
      
      <P ID="pudge">
        Built with
        <A HREF="http://lesscode.org/projects/pudge/">
          Pudge/0.1.3</A>
      </P>

      
    </DIV>
    </DIV>
  </BODY>

</HTML>